/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package victoralbertos.io.ohos.provider;

import com.bumptech.glide.Glide;
import io.rx_cache.entities.UserWithSource;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Image;
import victoralbertos.io.ohos.ResourceTable;

import java.util.ArrayList;
import java.util.List;

/**
 * 图片列表
 *
 * @author name
 * @since 2021-05-31
 */
public class ListProvider extends BaseItemProvider {
    private List<UserWithSource> users = new ArrayList<>();
    private AbilitySlice mSlice;

    /**
     * 构造方法
     *
     * @param users
     * @param context
     */
    public ListProvider(List<UserWithSource> users, AbilitySlice context) {
        this.users = users;
        this.mSlice = context;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int ii) {
        return users.get(ii);
    }

    @Override
    public long getItemId(int ii) {
        return ii;
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setData(List<UserWithSource> data) {
        users.clear();
        users.addAll(data);
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component cpt = component;
        ViewHolder viewHolder = null;
        if (cpt == null) {
            cpt = LayoutScatter.getInstance(mSlice).parse(ResourceTable.Layout_item_list, null, false);
            viewHolder = new ViewHolder();
            viewHolder.ivAvatar = (Image) cpt.findComponentById(ResourceTable.Id_iv_avatar);
            viewHolder.tvSource = (Text) cpt.findComponentById(ResourceTable.Id_tv_source);
            viewHolder.tvName = (Text) cpt.findComponentById(ResourceTable.Id_tv_name);
            cpt.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) cpt.getTag();
        }
        if (viewHolder != null) {
            final UserWithSource userWithSource = users.get(position);
            viewHolder.ivAvatar.setScaleMode(Image.ScaleMode.CENTER);
            Glide.with(mSlice)
                    .asDrawable()
                    .load(userWithSource.getUser().getAvatarUrl())
                    .into(viewHolder.ivAvatar);
            viewHolder.tvSource.setText(userWithSource.getSource());
            viewHolder.tvName.setText(userWithSource.getUser().getLogin());
        }
        return cpt;
    }

    /**
     * ViewHolder
     *
     * @author name
     * @since 2021-05-31
     */
    private static class ViewHolder {
        Image ivAvatar;
        Text tvSource;
        Text tvName;
    }
}
